﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_OrderDetail_ViewOrderDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        if (!IsPostBack)
        {
            if (Request["sDate"] == null && Request["eDate"] == null) //Truong hop don dat hang theo ngay
            {
                Picker1.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                Picker2.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            }
            else if (Request["sDate"] == "-1" && Request["eDate"] == "-1")
            {
                //Picker1.SelectedDate = null;
                // Picker2.SelectedDate = null;
            }
            else //Truong hop don dat hang theo thang
            {
                int mysDay, mysMonth, mysYear;
                int myeDay, myeMonth, myeYear;
                mysDay = int.Parse(Request["sDate"].ToString().Substring(6, 2));
                mysMonth = int.Parse(Request["sDate"].ToString().Substring(4, 2));
                mysYear = int.Parse(Request["sDate"].ToString().Substring(0, 4));

                myeDay = int.Parse(Request["eDate"].ToString().Substring(6, 2));
                myeMonth = int.Parse(Request["eDate"].ToString().Substring(4, 2));
                myeYear = int.Parse(Request["eDate"].ToString().Substring(0, 4));
                Picker1.SelectedDate = new DateTime(mysYear, mysMonth, mysDay);
                Picker2.SelectedDate = new DateTime(myeYear, myeMonth, myeDay);
            }
            BindState();
            if (Request.QueryString["sOrderID"] != "" && Request.QueryString["sOrderID"] != null)
                txtDDH.Text = Request.QueryString["sOrderID"].ToString();
            Bind_Order();


        }
    }
    private void BindState()
    {
        if (Request.QueryString["sState"] != "" && Request.QueryString["sState"] != null)
        {
            ddlState.SelectedItem.Selected = false;
            ddlState.Items.FindByValue(Request.QueryString["sState"]).Selected = true;
        }
    }



    private void Bind_Order()
    {
        clsWebShop cls = new clsWebShop();
        //Lay so luong trong kho
        Session["dtCountStore"] = cls.Get_QuantityStoreGroup("", "0").Tables[0];
        string sDateStart, sDateEnd;
        DataTable dt = new DataTable();
        string sDate = "", eDate = "";
        if (Picker1.SelectedDate.ToString("yyyyMMdd") != "00010101")
            sDate = Picker1.SelectedDate.ToString("yyyyMMdd");
        if (Picker2.SelectedDate.ToString("yyyyMMdd") != "00010101")
            eDate = Picker2.SelectedDate.ToString("yyyyMMdd");

        sDateStart = sDate;
        sDateEnd = eDate;
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("StartDate", sDateStart);
        msg.CreateFirstNode("EndDate", sDateEnd);
        msg.CreateFirstNode("Status", ddlState.SelectedValue);
        msg.CreateFirstNode("End_MK", "");
        msg.CreateFirstNode("OrderID", txtDDH.Text);
        dt = cls.Get_OrderPDByCondition(msg.GetXmlStr.ToString()).Tables[0];

        BindData(dt);



    }

    private void BindData(DataTable dt)
    {
        grvMaster.DataSource = dt;
        grvMaster.DataBind();
        string sCssTotal = "cssBorderGrv";
        if (dt.Rows.Count > 0)
        {
            TableCell cell = new TableCell();
            int m = 1;
            int n = 0;
            string sQuantity = "", sPrice = "", sTotal = "", striName = "";
            decimal TQuantity = 0, TPrice = 0, TTotal = 0;

            for (int i = 0; i < grvMaster.Rows.Count; )
            {
                decimal dQuantity = 0, dPrice = 0, dTotal = 0;
                striName = dt.Rows[i]["OrderID"].ToString();
                sQuantity = dt.Rows[i]["Quantity"].ToString();
                sPrice = dt.Rows[i]["Price"].ToString();
                sTotal = dt.Rows[i]["Total"].ToString();

                if (sQuantity == "")
                    sQuantity = "0";

                if (sPrice == "")
                    sPrice = "0";

                if (sTotal == "")
                    sTotal = "0";

                dQuantity = decimal.Parse(sQuantity);
                dPrice = decimal.Parse(sPrice);
                dTotal = decimal.Parse(sTotal);

                int kt = 1;
                for (int j = i + 1; j < dt.Rows.Count; j++)
                {
                    string strjName = dt.Rows[j]["OrderID"].ToString();
                    if (striName == strjName)
                    {
                        sQuantity = dt.Rows[j]["Quantity"].ToString();
                        sPrice = dt.Rows[j]["Price"].ToString();
                        sTotal = dt.Rows[j]["Total"].ToString();
                        if (sQuantity == "")
                            sQuantity = "0";

                        if (sPrice == "")
                            sPrice = "0";

                        if (sTotal == "")
                            sTotal = "0";

                        dQuantity += decimal.Parse(sQuantity);
                        dPrice += decimal.Parse(sPrice);
                        dTotal += decimal.Parse(sTotal);

                        kt += 1;
                        grvMaster.Rows[i].Cells[0].RowSpan = kt;
                        grvMaster.Rows[i].Cells[1].RowSpan = kt;
                        grvMaster.Rows[j].Cells.RemoveAt(0);
                        grvMaster.Rows[j].Cells.RemoveAt(0);
                    }
                    else
                        break;

                }
                TQuantity += dQuantity;
                TPrice += dPrice;
                TTotal += dTotal;


                GridViewRow gr = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
                cell = new TableCell();
                cell.Text = "<font color='Maroon'><b>Mã ĐĐH</b>:" + dt.Rows[i]["OrderID"].ToString() + "  <b>Ngày:</b>" + FormatDateTime(dt.Rows[i]["UpdDate"].ToString()) + " </font><font color='Navy'> <b>Tổng cộng-></b></font>";
                cell.ColumnSpan = 3;
                cell.CssClass = sCssTotal;
                gr.Cells.Add(cell);


                cell = new TableCell();
                cell.Text = dQuantity.ToString();
                cell.HorizontalAlign = HorizontalAlign.Right;
                cell.CssClass = sCssTotal;
                gr.Cells.Add(cell);

                cell = new TableCell();
                cell.HorizontalAlign = HorizontalAlign.Right;
                cell.CssClass = sCssTotal;
                gr.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = FormatMoney(dTotal);
                //cell.Text = dTotal.ToString();
                cell.HorizontalAlign = HorizontalAlign.Right;
                cell.CssClass = sCssTotal;
                gr.Cells.Add(cell);

                cell = new TableCell();
                cell.HorizontalAlign = HorizontalAlign.Right;
                cell.CssClass = sCssTotal;
                gr.Cells.Add(cell);

                cell = new TableCell();
                cell.HorizontalAlign = HorizontalAlign.Right;
                cell.CssClass = sCssTotal;
                gr.Cells.Add(cell);


                n = i + kt;

                gr.CssClass = "gvAlternatingRowFooter";

                grvMaster.Controls[0].Controls.AddAt(n + m, gr);
                m += 1;
                i += kt;
            }

            GridViewRow grF = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
            TableCell tbF = new TableCell();
            tbF.Text = "Tổng kết";
            tbF.ColumnSpan = 3;
            tbF.CssClass = sCssTotal;
            grF.Cells.Add(tbF);

            cell = new TableCell();
            cell.Text = TQuantity.ToString();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.CssClass = sCssTotal;
            grF.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.CssClass = sCssTotal;
            grF.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = FormatMoney(TTotal);
            //cell.Text = TTotal.ToString();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.CssClass = sCssTotal;
            grF.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.CssClass = sCssTotal;
            grF.Cells.Add(cell);

            cell = new TableCell();
            cell.HorizontalAlign = HorizontalAlign.Right;
            cell.CssClass = sCssTotal;
            grF.Cells.Add(cell);

            grF.CssClass = "gvFooter";
            grvMaster.Controls[0].Controls.Add(grF);
            grvMaster.BorderWidth = Unit.Pixel(1);

        }
        else
        {
            grvMaster.BorderWidth = Unit.Pixel(0);
        }

    }
    protected void grvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label hplHoten = new Label();
        HtmlImage imgSave = new HtmlImage();

        switch (e.Row.RowType)
        {
            case DataControlRowType.DataRow:
                string sCheckM_K = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
                string KindNo = DataBinder.Eval(e.Row.DataItem, "KindNo").ToString();
                if (KindNo != "P")
                {
                    e.Row.Cells[6].Text = "Free";
                    e.Row.Cells[6].BackColor = System.Drawing.Color.Yellow;
                    e.Row.Cells[4].BackColor = System.Drawing.Color.Yellow;
                    e.Row.Cells[4].Text = "Free";
                }
                else
                {
                    string sQ_Store = "0";
                    try
                    {
                        DataTable dt = (DataTable)Session["dtCountStore"];
                        sQ_Store = dt.Select(" PDID='" + DataBinder.Eval(e.Row.DataItem, "PDID").ToString() + "'")[0]["Quantity_Store"].ToString();
                    }
                    catch
                    {
                        sQ_Store = "0";
                    }
                    e.Row.Cells[6].Text = sQ_Store;
                    if (int.Parse(sQ_Store) < int.Parse(e.Row.Cells[3].Text))
                    {

                        e.Row.Cells[3].BackColor = System.Drawing.Color.FromName("red");
                    }

                    e.Row.Cells[4].Text = e.Row.Cells[4].Text + " <font color='red'>" + DataBinder.Eval(e.Row.DataItem, "Rate").ToString() + " </font>";
                }
                string End_MK = DataBinder.Eval(e.Row.DataItem, "End_MK").ToString();
                string U_Status = DataBinder.Eval(e.Row.DataItem, "U_Status").ToString();
                if (End_MK == "D") //Don & KH dang o trang thai xoa
                {
                    e.Row.BackColor = System.Drawing.Color.Red;
                    //e.Row.Cells[0].Enabled = false;
                }
                if (U_Status == "D")
                {
                    e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
                    //e.Row.Cells[0].Enabled = false;
                }
                imgSave = (HtmlImage)(e.Row.FindControl("imgSave"));
                switch (sCheckM_K)
                {
                    case "Y":
                        imgSave.Src = ResolveUrl("~/Images/Icon/ico_success.gif");
                        break;
                    case "C":
                        imgSave.Src = ResolveUrl("~/Images/Icon/clock.gif");
                        break;
                    case "N":
                        imgSave.Src = ResolveUrl("~/Images/Icon/ico_running.gif");
                        break;
                    case "E":
                        imgSave.Src = ResolveUrl("~/Images/Icon/sold.jpg");
                        break;
                }



                e.Row.Cells[0].Text = "<a style='color:Navy;cursor:pointer' href='#' onclick='fnPrintOrder(\"" + Request.QueryString["CodeID"] + "\",\"" + DataBinder.Eval(e.Row.DataItem, "OrderID").ToString() + "\",\"" + DataBinder.Eval(e.Row.DataItem, "Status").ToString() + "\")') ><img src='../../Images/Icon/cmdPrint.gif' border='0'></a>";

                hplHoten = (Label)(e.Row.Cells[0].FindControl("hplHoten"));
                hplHoten.Text = "<a style='color:Navy;cursor:pointer' href='SignOrderPD.aspx?CodeID=" + Request.QueryString["CodeID"] + "&sDate=" + Picker1.SelectedDate.ToString("yyyyMMdd") + "&eDate=" + Picker2.SelectedDate.ToString("yyyyMMdd") + "&sState=" + ddlState.SelectedValue + "&sOrderID=" + txtDDH.Text + "&OrderID=" + DataBinder.Eval(e.Row.DataItem, "OrderID").ToString() + "&State=" + DataBinder.Eval(e.Row.DataItem, "Status").ToString() + "'><font color='Navy'>" + DataBinder.Eval(e.Row.DataItem, "CusNm").ToString().ToUpper() + "</font></a>";



                // e.Row.Cells[3].Text = cls.FormatMoney(e.Row.Cells(3).Text);
                //e.Row.Cells[4].Text = cls.FormatMoney(e.Row.Cells(4).Text);
                e.Row.Cells[3].Text = e.Row.Cells[3].Text;

                break;
            case DataControlRowType.EmptyDataRow:

                e.Row.Cells[0].Text = "<span style='background-color:yellow;font-size:12px;font-weight:bold'>Không tìm thấy loại đơn đặt hàng nào trong ngày này</span>";
                e.Row.HorizontalAlign = HorizontalAlign.Center;
                e.Row.BorderWidth = Unit.Empty;
                e.Row.Cells[0].Height = Unit.Pixel(200);
                break;

        }
    }

    private string FormatMoney(string sNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", decimal.Parse(sNum));
        }
        catch (Exception) { }
        return sReturn;
    }


    private string FormatMoney(decimal dNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", dNum);
        }
        catch (Exception) { }
        return sReturn;
    }
    private string FormatDate(string sDate)
    {
        string sReturn = "";
        try
        {
            sReturn = sDate.ToString().Substring(6, 2) + "/" + sDate.ToString().Substring(4, 2) + "/" + sDate.ToString().Substring(0, 4);
        }
        catch (Exception ex) { }

        return sReturn;
    }
    private string FormatDateTime(string sDate)
    {
        string sReturn = "";
        try
        {
            sReturn = sDate.ToString().Substring(6, 2) + "/" + sDate.ToString().Substring(4, 2) + "/" + sDate.ToString().Substring(0, 4);
            sReturn += "  " + sDate.ToString().Substring(8, 2) + ":" + sDate.ToString().Substring(10, 2) + ":" + sDate.ToString().Substring(12, 2);
        }
        catch (Exception ex) { }

        return sReturn;
    }
    #region "Export_ToExcel"
    private void ExportToExcel()
    {
        clsCommon clsCom = new clsCommon();
        grvMaster.EnableViewState = false;
        // Set the content type to Excel.
        Response.Clear();
        Response.Buffer = true;

        Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;
        Response.AppendHeader("Content-disposition", "attachmentfilename=DetailOrderDay.xls");
        Response.ContentType = "application/vnd.ms-excel";
        // / "application/x-msexcel"

        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        hw.WriteFullBeginTag("HTML");
        hw.WriteFullBeginTag("HEAD");

        string sStyle = "";

        sStyle += " .MergeCellRow{background:#e2ffcc;color:Black;font-weight:bold;font-size:10pt;font-family:Verdana;vertical-align:middle;text-align:center;border-top-color: #407db3;border-left-color: #407db3;border-right-color: #407db3;border-bottom-color: #407db3;}";
        sStyle += ".cssBorderGrv{border-top-color: #407db3;border-left-color: #407db3;border-right-color: #407db3;border-bottom-color: #407db3;}";
        sStyle += ".gvRowStyle{color:Black;	background:#feffeb;vertical-align:middle;border-top-color: black;border-left-color: black;border-right-color: black;border-bottom-color: black;}";
        sStyle += ".cssHeader{height:30px;BACKGROUND-COLOR: #D8ECFF;text-align:center;color:#000000;font-weight:bold;}";
        sStyle += ".gvAlternatingRowStyle{color:Black;background:#ffffff;vertical-align:middle;border-top-color: black;border-left-color: black;border-right-color: black;border-bottom-color: black;}";
        sStyle += ".gvAlternatingRowFooter{background:#f5e38a;color:Black;font-size:10pt;font-family:Verdana;vertical-align:middle;border-top-color: Black;border-left-color: Black;border-right-color: Black;border(-bottom - Color) : Black()}";
        sStyle += ".gvFooter{background:Silver;color:Black;font-weight:bold;font-size:10pt;font-family:Verdana;	vertical-align:middle;border-top-color: black;border-left-color: black;border-right-color: black;border-bottom-color: black;}";
        sStyle += ".kindmk2_total_value { COLOR: #000000 BACKGROUND-COLOR: #ccffffBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solid TEXT-ALIGN: rightVERTICAL-ALIGN:middle }";
        sStyle += ".sharehour { COLOR: #ff3300 BACKGROUND-COLOR: #ffff00 TEXT-ALIGN: rightBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solid}";
        sStyle += ".sharehourfee {COLOR: #ff3300 BACKGROUND-COLOR: #ffff00 TEXT-ALIGN: rightBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solidFONT-WEIGHT: bold}";
        sStyle += ".sharehourfee1 {COLOR: #ff3300 BACKGROUND-COLOR: #ffff00 TEXT-ALIGN: centerBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solidFONT-WEIGHT: bold}";
        sStyle += ".sharefee { COLOR: #ff3300 BACKGROUND-COLOR: #ffff00 TEXT-ALIGN: right }";
        sStyle += ".sharefee1 { COLOR: #ff3300 BACKGROUND-COLOR: #ffff00TEXT-ALIGN: centerBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solidFONT-WEIGHT: bold}";
        sStyle += ".sharefee2 { COLOR: #ff3300 BACKGROUND-COLOR: #ffff00TEXT-ALIGN: leftBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solidFONT-WEIGHT: bold}";
        sStyle += ".fd_sharefee {COLOR: #ff3300 BACKGROUND-COLOR: #ffff00 TEXT-ALIGN: centerBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solidFONT-WEIGHT: bold}";
        sStyle += ".value_left{COLOR: #000000 BACKGROUND-COLOR: #ffffffTEXT-ALIGN:leftBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solid}.value_right{COLOR: #000000 BACKGROUND-COLOR: #ffffffTEXT-ALIGN:rightBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solid}.rb_center{TEXT-ALIGN:centerBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solid}.fdb_header_stt{COLOR: #000000 BACKGROUND-COLOR: #99ccff TEXT-ALIGN: centerBORDER-RIGHT: #000000 1px solid BORDER-BOTTOM: #000000 1px solidBORDER-TOP:#000000 1px solidBORDER-TOP:#000000 1px solid}";

        hw.RenderBeginTag(HtmlTextWriterTag.Style);
        hw.Write("<!--");
        hw.WriteLine(sStyle);
        hw.Write("-->");
        hw.RenderEndTag();

        hw.WriteEndTag("HEAD");
        hw.WriteFullBeginTag("BODY");

        // Get the HTML for the control	
        hw.WriteFullBeginTag("DIV align=center");
        hw.Write("<span style=color:Red; size:8pt>CHI TIẾT ĐƠN ĐẶT HÀNG NGÀY " + clsCom.FormatDate(DateTime.Now.ToString("dd/MM/yyyy")) + "</span>");
        hw.WriteEndTag("DIV");

        HtmlForm frm = new HtmlForm();
        this.Controls.Clear();
        this.Controls.Add(frm);

        frm.Controls.Add(grvMaster);

        frm.RenderControl(hw);

        hw.WriteEndTag("BODY");
        hw.WriteEndTag("HTML");

        // Write the HTML back to the browser.
        Response.Write(tw.ToString());
        Response.Flush();
        // End the response.
        Response.End();
    }
    #endregion
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind_Order();
    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind_Order();
    }
    protected void imgExportExcel_Click(object sender, ImageClickEventArgs e)
    {
        Bind_Order();
        ExportToExcel();
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        bool flagNone = true;
        string sOrderID = "";
        for (int i = 0; i < grvMaster.Rows.Count; i++)
        {
            CheckBox chkApprove = (CheckBox)grvMaster.Rows[i].FindControl("chkApprove");

            if (chkApprove.Checked == true)
            {
                /*string sOrderID = grvMaster.DataKeys[i].Value.ToString();
                cls.UpdateData("update OrderPD set Status='Y',EndDate='" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + "' where  OrderID='" + sOrderID + "'");
                flagNone = false;*/

                sOrderID = grvMaster.DataKeys[i].Value.ToString();
                PccMsg msg = new PccMsg();
                msg.CreateFirstNode("OrderID", sOrderID);
                msg.CreateFirstNode("flag", "2");
                msg.CreateFirstNode("UpdID", Session["AdminUserID"].ToString());
                string xml = cls.Upd_SignPD(msg.GetXmlStr);
                msg = new PccMsg(xml);
                string strOutput = msg.Query("strOutput");
                if (strOutput == "-2")
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Trong đơn đặt hàng " + sOrderID + ". Sản phẩm trong kho không đủ số lượng')</script>");
                    Bind_Order();
                    return;
                }
                flagNone = false;
            }
        }
        if (flagNone == true)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Chọn đơn')</script>");
            Bind_Order();
            return;
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Đã duyệt thành công')</script>");
        Bind_Order();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        bool flagNone = true;
        for (int i = 0; i < grvMaster.Rows.Count; i++)
        {
            CheckBox chkApprove = (CheckBox)grvMaster.Rows[i].FindControl("chkApprove");
            if (chkApprove.Checked == true)
            {
                string sOrderID = grvMaster.DataKeys[i].Value.ToString();
                cls.UpdateData("update OrderPD set Status='C',EndDate='" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + "' where  OrderID='" + sOrderID + "'");
                flagNone = false;
            }
        }
        if (flagNone == true)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Chọn đơn')</script>");
            Bind_Order();
            return;
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Hủy bỏ thành công')</script>");
        Bind_Order();
    }





    protected void Button2_Click(object sender, EventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg;
        bool flagNone = true;
        string sOrderID = "";
        string sDateTime = System.DateTime.Now.ToString("yyyyMMddhhmmss");
        try
        {

            for (int i = 0; i < grvMaster.Rows.Count; i++)
            {
                CheckBox chkApprove = (CheckBox)grvMaster.Rows[i].FindControl("chkApprove");
                if (chkApprove.Checked == true)
                {
                    sOrderID = grvMaster.DataKeys[i].Value.ToString();
                    msg = new PccMsg();
                    msg.CreateFirstNode("OrderID", sOrderID);
                    msg.CreateFirstNode("flag", "1");
                    msg.CreateFirstNode("UpdID", Session["AdminUserID"].ToString());
                    flagNone = false;
                    string xml = cls.Upd_SignPD(msg.GetXmlStr);
                    msg = new PccMsg(xml);
                    string strOutput = msg.Query("strOutput");
                    if (strOutput == "-2")
                    {
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Trong đơn đặt hàng " + sOrderID + ". Sản phẩm trong kho không đủ số lượng')</script>");
                        Bind_Order();
                        return;
                    }
                }

            }

            if (flagNone == true)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Chọn đơn')</script>");
                Bind_Order();
                return;
            }
        }
        catch (Exception ex)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('" + ex.Message.ToString() + "')</script>");
            Bind_Order();
            return;
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Đã xác nhận thành công')</script>");
        Bind_Order();
    }
}
